﻿//收付款账单管理
var gselfUrl = "../ReceivablePayable/GetJsonOfBills";
var columnsSK =
            [
                { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                { display: '客户名称', name: 'ClientName', mintWidth: 10, width: 160 }, //, type: 'int', mintWidth: 40, width: 100
                {display: '账单分类', name: 'BillClassifyName', mintWidth: 60, width: 60 },
                { display: '账单类别', name: 'BillTypeName', mintWidth: 80, width: 140 },
                 { display: '组织机构', name: 'OrgName', mintWidth: 80, width: 140 },
                { display: '应收款(元)', name: 'BillReceivable', mintWidth: 100, width: 100, type: 'float' },
                { display: '已收款(元)', name: 'BillReceiv', mintWidth: 100, width: 100, type: 'float' },
                { display: '应收优惠(元)', name: 'BillReceivPft', mintWidth: 100, width: 100, type: 'float' },
//                { display: '应付款', name: 'BillPayable', mintWidth: 150, width: 200 },
//                { display: '已付款', name: 'BillPay', mintWidth: 150, width: 200 },
//                { display: '应付优惠', name: 'BillPayPft', mintWidth: 150, width: 200 },
             //   { display: '删除状态', name: 'DeleteState', mintWidth: 80, width: 140 },
                {display: '收款状态', name: 'BillStateName', mintWidth: 80, width: 120 },
                { display: '备注', name: 'Remarks',mintWidth: 100, width: 160 }
             ];
var columnsFK =
            [
                { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                { display: '客户名称', name: 'ClientName', mintWidth: 100, width: 160 }, //, type: 'int', mintWidth: 40, width: 100
                {display: '账单分类', name: 'BillClassifyName', mintWidth: 60, width: 60 },
                { display: '账单类别', name: 'BillTypeName', mintWidth: 80, width: 140 },
                { display: '组织机构', name: 'OrgName', mintWidth: 80, width: 140 },
//                { display: '应收款(元)', name: 'BillReceivable', mintWidth: 80, width: 120, type: 'float' },
//                { display: '已收款', name: 'BillReceiv', mintWidth: 150, width: 200 },
//                { display: '应收优惠', name: 'BillReceivPft', mintWidth: 150, width: 200 },
                {display: '应付款(元)', name: 'BillPayable', mintWidth: 100, width: 100, type: 'float' },
                { display: '已付款(元)', name: 'BillPay', mintWidth: 100, width: 100, type: 'float' },
                { display: '应付优惠(元)', name: 'BillPayPft', mintWidth: 100, width: 100, type: 'float' },
              //  { display: '删除状态', name: 'DeleteState', mintWidth: 80, width: 140 },
                {display: '应付状态', name: 'BillStateName', mintWidth: 80, width: 120 },
                { display: '备注', name: 'Remarks', mintWidth: 100, width: 160 }
             ];
var manager;
//init controls
var orgCode = "";//组织机构代码
$(document).ready(function () {
    $("#btnSearch").unbind("click").bind("click", initligerGrid);
    initligerGrid();
});
function getOrgCode() {
    var $obj = $("#HidOrgCode");
    if ($obj != null && $obj != undefined) {
        if ($obj.val() != undefined && $obj.val() != null && $.trim($obj.val()).length > 0) {
            return $obj.val();
        }
    }
    return "";
}
function initligerGrid() {
    
    manager = null;
    manager = $("#ReceivablePayableGrid").ligerGrid({
        columns: $("#billSort").val() == "0" ? columnsSK : columnsFK,
        title: '收付款账单管理',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: '99%',
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [
                                    { text: '添加', click: dialogAdd, icon: 'add' },
                                     { line: true },
                                    { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' }
                                    ]
        },
        parms: { orgCode: getOrgCode(), sortCode: $("#billSort").val(), typeCode: $("#BillType").val() },
        url: gselfUrl,
        onDblClickRow: function (data, rowindex, rowobj) { dialogView(data.ID); }
    });
}

//detail
function dialogDetailView() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择要查看的行!'); return; }
    var billId = row["ID"].toString();
    dialogView(billId);
}
function dialogView(billId) {
    $.ligerDialog.open({ url: "../ReceivablePayable/Detail?id=" + billId, height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false });
}
//add
function dialogAdd() {
    $.ligerDialog.open({ url: "../ReceivablePayable/InitFinal?orgCode=" + $("#HidOrgCode").val(), height: getDialogHeight(), width: getDialogWidth(),
        isResize: true, isHidden: false
    });
}
//delete
function deleteOperation() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择需要删除行！'); return; }
    $.ligerDialog.confirm('确定要删除吗？', function (yes) {
        if (yes) {
            $.get("../ReceivablePayable/DeleteBill?billId=" + row["ID"].toString(), {}, function (data) {
                if (data != undefined && data == "9") {
                    initligerGrid();
                }
                else if (data == "-1") {
                    $.ligerDialog.success('账务已开始进行收付款操作，不能删除！'); return;
                }
                else {
                    $.ligerDialog.success('删除异常，无法删除！'); return;
                }
            });
        }
    });
}